package customer.zspt.cn.customer.utils;

import customer.zspt.cn.customer.bean.GetGroupChatMsgDao;
import customer.zspt.cn.customer.constant.chatMsgFieldName;
import customer.zspt.cn.customer.sqlites.dao.chatMsgSqliteDao;

/**
 * Created by Administrator on 2018/8/23 0023.
 */

public class AddDateToSqliteUtils {

    /**
     * 增加数据进数据库
     */
    public static void addMsgToSqlite(GetGroupChatMsgDao getgroupChatMsgDao, chatMsgSqliteDao msgSqliteDao) {
        String avatar = getgroupChatMsgDao.data.avatar != null ? getgroupChatMsgDao.data.avatar : "";
        String content = getgroupChatMsgDao.data.content != null ? getgroupChatMsgDao.data.content : "";
        String fromid = getgroupChatMsgDao.data.fromid != null ? getgroupChatMsgDao.data.fromid : getgroupChatMsgDao.data.id;
        String groupavatar = getgroupChatMsgDao.data.groupavatar != null ? getgroupChatMsgDao.data.groupavatar : "";
        String groupname = getgroupChatMsgDao.data.groupname != null ? getgroupChatMsgDao.data.groupname : "";
        long timestamp = getgroupChatMsgDao.data.timestamp != 0 ? getgroupChatMsgDao.data.timestamp : System.currentTimeMillis();
        String userName = getgroupChatMsgDao.data.username != null ? getgroupChatMsgDao.data.username : "";
        String chatobjAvater = "", chatobjName = "";
        //判断是不是群聊
        if (groupavatar.equals("")) {
            chatobjAvater = avatar;
            chatobjName = userName;
        } else {
            chatobjAvater = getgroupChatMsgDao.data.groupavatar;
            chatobjName = getgroupChatMsgDao.data.groupname;
        }
        int type = MsgType(content);
        String dateTime = TimestampToBj.getTime(timestamp);
        String sql = SqlOptionsUtils.SqlStr(fromid, avatar, content, chatMsgFieldName.FROM,
                timestamp, userName,
                chatMsgFieldName.UNREAD,
                type, groupavatar, groupname, chatobjAvater, chatobjName,dateTime);
        msgSqliteDao.addNewTable(fromid, sql);
    }
    //判断消息类型
    private static int MsgType(String content) {
        String[] split = content.split("\\[");
        switch (split[0]) {
            case "img":
                return chatMsgFieldName.IMAGE;
            case "essay":
                return chatMsgFieldName.ESSAY;
            case "voice":
                return chatMsgFieldName.VOICE;
            case "video":
                return chatMsgFieldName.VIDEO;
            case "file":
                return chatMsgFieldName.FILE;
            default:
                return chatMsgFieldName.CONTENT;
        }
    }
}
